class Solution(object):
    def wordPattern(self, pattern, str):
        """
        :type pattern: str
        :type str: str
        :rtype: bool
        """
        dict1, dict2 = {}, {}
        strs = str.split()
        if len(pattern)!=len(strs):return False
        for i in range(len(pattern)):
            ch = ord(pattern[i])
            s = strs[i]
            if ch not in dict1 and s not in dict2:
                dict1.update({ch:s})
                dict2.update({s:ch})
            elif ch in dict1 and dict1[ch]==s and s in dict2 and dict2[s]==ch:
                continue
            else:
                return False
        return True

print(Solution().wordPattern("abba", "dog cat cat dog"))